layui.define(['layer', 'form', 'laytpl'], function (exports) {
  "use strict";

  let $ = layui.$
      , form = layui.form
      , layarea = {
    _id: 0
    , config: {}
    , set: function (options) {
      let that = this;
      that.config = $.extend({}, that.config, options);
      return that;
    }
    , on: function (events, callback) {
      return layui.onevent.call(this, 'layarea', events, callback);
    }
  }
      , thisArea = function () {
    let that = this;
    return {
      layarea: function (files) {
        that.layarea.call(that, files);
      }
      , config: that.config
    }
  }
      , Class = function (options) {
    let that = this;
    that.config = $.extend({}, that.config, layarea.config, options);
    that.render();
  };

  let areaList = {
    province_list: {
      210000: '辽宁省',
    },
    xZS_list: {
      210100: '沈阳市',
      210200: '大连市',
      210300: '鞍山市',
      210400: '抚顺市',
      210500: '本溪市',
      210600: '丹东市',
      210700: '锦州市',
      210800: '营口市',
      210900: '阜新市',
      211000: '辽阳市',
      211100: '盘锦市',
      211200: '铁岭市',
      211300: '朝阳市',
      211400: '葫芦岛市',
    },
    xZX_list: {
      210102: '和平区',
      210103: '沈河区',
      210104: '大东区',
      210105: '皇姑区',
      210106: '铁西区',
      210111: '苏家屯区',
      210112: '浑南区',
      210113: '沈北新区',
      210114: '于洪区',
      210115: '辽中区',
      210123: '康平县',
      210124: '法库县',
      210181: '新民市',
      210190: '经济技术开发区',
      210202: '中山区',
      210203: '西岗区',
      210204: '沙河口区',
      210211: '甘井子区',
      210212: '旅顺口区',
      210213: '金州区',
      210214: '普兰店区',
      210224: '长海县',
      210281: '瓦房店市',
      210283: '庄河市',
      210302: '铁东区',
      210303: '铁西区',
      210304: '立山区',
      210311: '千山区',
      210321: '台安县',
      210323: '岫岩满族自治县',
      210381: '海城市',
      210390: '高新区',
      210402: '新抚区',
      210403: '东洲区',
      210404: '望花区',
      210411: '顺城区',
      210421: '抚顺县',
      210422: '新宾满族自治县',
      210423: '清原满族自治县',
      210502: '平山区',
      210503: '溪湖区',
      210504: '明山区',
      210505: '南芬区',
      210521: '本溪满族自治县',
      210522: '桓仁满族自治县',
      210602: '元宝区',
      210603: '振兴区',
      210604: '振安区',
      210624: '宽甸满族自治县',
      210681: '东港市',
      210682: '凤城市',
      210702: '古塔区',
      210703: '凌河区',
      210711: '太和区',
      210726: '黑山县',
      210727: '义县',
      210781: '凌海市',
      210782: '北镇市',
      210793: '经济技术开发区',
      210802: '站前区',
      210803: '西市区',
      210804: '鲅鱼圈区',
      210811: '老边区',
      210881: '盖州市',
      210882: '大石桥市',
      210902: '海州区',
      210903: '新邱区',
      210904: '太平区',
      210905: '清河门区',
      210911: '细河区',
      210921: '阜新蒙古族自治县',
      210922: '彰武县',
      211002: '白塔区',
      211003: '文圣区',
      211004: '宏伟区',
      211005: '弓长岭区',
      211011: '太子河区',
      211021: '辽阳县',
      211081: '灯塔市',
      211102: '双台子区',
      211103: '兴隆台区',
      211104: '大洼区',
      211122: '盘山县',
      211202: '银州区',
      211204: '清河区',
      211221: '铁岭县',
      211223: '西丰县',
      211224: '昌图县',
      211281: '调兵山市',
      211282: '开原市',
      211302: '双塔区',
      211303: '龙城区',
      211321: '朝阳县',
      211322: '建平县',
      211324: '喀喇沁左翼蒙古族自治县',
      211381: '北票市',
      211382: '凌源市',
      211402: '连山区',
      211403: '龙港区',
      211404: '南票区',
      211421: '绥中县',
      211422: '建昌县',
      211481: '兴城市',
      215090: '工业园区',
    }
  };


  Class.prototype.config = {
    elem: '',
    data: {
      province: '--选择省--',
      xZS: '--选择市--',
      xZX: '--选择区--',
    },
    change: function(result){}
  };

  Class.prototype.index = 0;

  Class.prototype.render = function () {
    let that = this, options = that.config;
    options.elem = $(options.elem);
    options.bindAction = $(options.bindAction);

    that.events();
  };

  Class.prototype.events = function () {
    let that = this, options = that.config, index;
    let provinceFilter = 'province-' + layarea._id;
    let xZSFilter = 'xZS-' + layarea._id;
    let xZXFilter = 'xZX-' + layarea._id;

    let provinceEl = options.elem.find('.province-selector');
    let xZSEl = options.elem.find('.xZS-selector');
    let xZXEl = options.elem.find('.xZX-selector');

    //filter
    if(provinceEl.attr('lay-filter')){
      provinceFilter = provinceEl.attr('lay-filter');
    }
    if(xZSEl.attr('lay-filter')){
      xZSFilter = xZSEl.attr('lay-filter');
    }
    if(xZXEl.attr('lay-filter')){
      xZXFilter = xZXEl.attr('lay-filter');
    }
    provinceEl.attr('lay-filter', provinceFilter);
    xZSEl.attr('lay-filter', xZSFilter);
    xZXEl.attr('lay-filter', xZXFilter);

    //获取默认值
    if(provinceEl.data('value')){
      options.data.province = provinceEl.data('value');
    }
    if(xZSEl.data('value')){
      options.data.xZS = xZSEl.data('value');
    }
    if(xZXEl.data('value')){
      options.data.xZX = xZXEl.data('value');
    }
    provinceEl.attr('lay-filter', provinceFilter);
    xZSEl.attr('lay-filter', xZSFilter);
    xZXEl.attr('lay-filter', xZXFilter);

    //监听结果
    form.on('select('+provinceFilter+')', function(data){
      options.data.province = data.value;
      let code = getCode('province', data.value);
      renderxZS(code);

      options.change(options.data);
    });
    form.on('select('+xZSFilter+')', function(data){
      options.data.xZS = data.value;
      let code = getCode('xZS', data.value);
      renderxZX(code);

      options.change(options.data);
    });
    form.on('select('+xZXFilter+')', function(data){
      options.data.xZX = data.value;

      options.change(options.data);
    });

    renderProvince();

    //查找province
    function renderProvince(){
      let tpl = '';
      let provinceList = getList("province");
      let currentCode = '';
      let currentName = '';
      provinceList.forEach(function(_item){
        if (!currentCode){
          currentCode = _item.code;
          currentName = _item.name;
        }
        if(_item.name === options.data.province){
          currentCode = _item.code;
          currentName = _item.name;
        }
        tpl += '<option value="'+_item.name+'">'+_item.name+'</option>';
      });
      options.data.province = currentName;
      provinceEl.html(tpl);
      provinceEl.val(options.data.province);
      form.render('select');
      renderxZS(currentCode);
    }

    function renderxZS(provinceCode){
      let tpl = '';
      let xZSList = getList('xZS', provinceCode.slice(0, 2));
      let currentCode = '';
      let currentName = '';
      xZSList.forEach(function(_item){
        if (!currentCode){
          currentCode = _item.code;
          currentName = _item.name;
        }
        if(_item.name === options.data.xZS){
          currentCode = _item.code;
          currentName = _item.name;
        }
        tpl += '<option value="'+_item.name+'">'+_item.name+'</option>';
      });
      options.data.xZS = currentName;
      xZSEl.html(tpl);
      xZSEl.val(options.data.xZS);
      form.render('select');
      renderxZX(currentCode);
    }

    function renderxZX(xZSCode){
      let tpl = '';
      let xZXList = getList('xZX', xZSCode.slice(0, 4));
      let currentCode = '';
      let currentName = '';
      xZXList.forEach(function(_item){
        if (!currentCode){
          currentCode = _item.code;
          currentName = _item.name;
        }
        if(_item.name === options.data.xZX){
          currentCode = _item.code;
          currentName = _item.name;
        }
        tpl += '<option value="'+_item.name+'">'+_item.name+'</option>';
      });
      options.data.xZX = currentName;
      xZXEl.html(tpl);
      xZXEl.val(options.data.xZX);

      form.render('select');
    }

    function getList(type, code) {
      let result = [];

      if (type !== 'province' && !code) {
        return result;
      }

      let list = areaList[type + "_list"] || {};
      result = Object.keys(list).map(function (code) {
        return {
          code: code,
          name: list[code]
        };
      });

      if (code) {
        // oversea code
        if (code[0] === '9' && type === 'xZS') {
          code = '9';
        }

        result = result.filter(function (item) {
          return item.code.indexOf(code) === 0;
        });
      }

      return result;
    }

    function getCode(type, name){
      let code = '';
      let list = areaList[type + "_list"] || {};
      layui.each(list, function(_code, _name){
        if(_name === name){
          code = _code;
        }
      });

      return code;
    }
  };

  layarea.render = function (options) {
    let inst = new Class(options);
    layarea._id++;
    return thisArea.call(inst);
  };

  //暴露接口
  exports('layarea', layarea);
});